package com.xk857.article.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xk857.article.mapper.provider.ArticleProvider;
import com.xk857.article.req.ArticleListREQ;
import com.xk857.entities.Article;
import com.xk857.feign.req.UserInfoREQ;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * 文章信息表 Mapper 接口
 *
 * @author CV大魔王
 * @since 2021-02-21
 */
public interface ArticleMapper extends BaseMapper<Article> {

    /**
     * 根据id查询文章详情与知识点
     *
     * @param id 文章id
     * @return 文章详情与知识点
     */
    @Select("SELECT id,user_id,nick_name,user_image,title,summary,image_url,md_content,html_content,view_count,thumhup,status,ispublic,create_date,update_date FROM xk857_article where id = #{id}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "labelIds", column = "id", javaType = List.class, many = @Many(select = "com.xk857.article.mapper.LabelMapper.findLabelIdByArticleId")),
            @Result(property = "labelList", column = "id", javaType = List.class, many = @Many(select = "com.xk857.article.mapper.LabelMapper.findLabelListByArticleId"))
    })
    Article findArticleAndLabelById(String id);


    /**
     * 通过文章 id 删除文章知识点中间表
     *
     * @param articleId 文章id
     */
    @Delete("DELETE FROM xk857_article_label where article_id = #{articleId}")
    void deleteArticleLabel(@Param("articleId") String articleId);

    /**
     * 新增文章知识点中间表数据
     *
     * @param articleId 文章id
     * @param labelIds 知识点id集合
     */
    @SelectProvider(type = ArticleProvider.class, method = "saveArticleLabel")
    void saveArticleLabel(@Param("articleId") String articleId, @Param("labelIds") List<String> labelIds);


    /**
     * 通过分类id或标签id查询文章列表
     *
     * @param page 分页数据
     * @param req 条件
     * @return 分页后的IPage<Article>
     */
    @SelectProvider(type = ArticleProvider.class, method = "findListByLabelIdOrCategoryId")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "labelIds", column = "id", javaType = List.class, many = @Many(select = "com.xk857.article.mapper.LabelMapper.findLabelIdByArticleId")),
            @Result(property = "labelList", column = "id", javaType = List.class, many = @Many(select = "com.xk857.article.mapper.LabelMapper.findLabelListByArticleId"))
    })
    IPage<Article> findListByLabelIdOrCategoryId(IPage<Article> page, @Param("req") ArticleListREQ req);


    /**
     * 统计各分类下的文章数
     * @return 各分类下的文章数
     */
    @Select("SELECT name, value FROM v_category_total")
    List<Map<String, Object>> selectCategoryTotal();


    /**
     * 统计近6个月发布的文章数
     * @return
     */
    @Select("SELECT `year_month` , `total` FROM v_month_aritcle_total")
    List<Map<String, Object>> selectMonthAritcleTotal();


    /**
     * 更新文章与评论表中的用户信息
     * @param req 封装的请求类信息
     * @return 逻辑值，是否修改成功
     */
    @Update({"UPDATE xk857_article SET nick_name = #{nickName}, user_image = #{userImage} WHERE user_id = #{userId}; UPDATE xk857_comment SET nick_name = #{nickName}, user_image = #{userImage} WHERE user_id = #{userId}"})
    boolean updateUserInfo(UserInfoREQ req);

    /**
     * 查询今日文章数量
     * @return 文章数量
     */
    @Select("select count(id) from xk857_article where to_days(update_date) = to_days(now())")
    Integer searchCountDayNum();


    /**
     * 查询昨日文章数量
     * @return 文章数量
     */
    @Select("SELECT count(id) FROM xk857_article WHERE TO_DAYS(NOW( )) - TO_DAYS(update_date) <= 1")
    Integer searchNumberOfArticlesInYesterdaySDay();


    /**
     * 查询近七天文章数量
     * @return
     */
    @Select("SELECT count(id) FROM xk857_article where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(update_date)")
    Integer searchNumberOfArticlesInWeek();
}
